// const { count } = require("console");

axios.interceptors.request.use(
    (config) => {
        config.headers['Authorization'] = 'Bearer' + ' ' + localStorage.getItem('token');
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)
axios.interceptors.response.use(
    (res) => {
        return res
    },
    (error) => {
        if (error.response.status == 401) {
            alert('登录过期，需要重新登录')
            localStorage.removeItem('token')
            location.href = './login.html'
        } else if (error.response.status == 404) {
            alert('访问路径有误')
        } else if (error.response.status == 500) {
            alert('服务器内部错误')
        }
        return Promise.reject(error)
    }
)

var searchBtn = document.querySelector('.search')
var inEle = document.querySelector('input')
var userinfo = JSON.parse(localStorage.getItem('userinfo'))
document.querySelector('.username').innerHTML = userinfo.phone

searchBtn.onclick = () => {
    axios.get('/user/search', { params: { searchkey: intEle.value } }).then((res) => {
        render(res.data.list)
        console.log(res.data);
    })
}



function render(data) {
    var str = ''
    num = 1
    row = 1
    str = data.map((item, index) => {
        return `<ul class="row">
        <li>${num++}</li>
        <li>${item.phone}</l>
        <li>${item.pass}</li>
        <li>${row++}</li>
        <li>正常</li>
        <li>201800000</li>
        
        <li><span class="iconfont icon-shanchu" id="101"></span></li>
    </ul>
        `
    }).join('')

    //* 如果没有数据
    if (str == '') {
        str = `<ul style="text-align:center;color:red">未查询到数据</ul>`
    }
    document.querySelector('.table-body').innerHTML = str
}
var page = 1
axios.get('/user/list', { params: { page: page, count: 5 } }).then((res) => {

    render(res.data.list)
    console.log(res.data);
})


var next = document.querySelector('.next')
next.onclick = function () {
    page++
    axios.get('/user/list', { params: { page: page, count: 5 } }).then((res) => {
        render(res.data.list)
    })

}
// 上一页
var prev = document.querySelector('.prev')
prev.onclick = function () {
    page--
    if (page <= 0) {
        page = 1
    }
    axios.get('/user/list', { params: { page: page, count: 5 } }).then((res) => {
        render(res.data.list)
    })
}

// var tabEle = document.querySelector('.table-body')
// tabEle.onclick = function (e) {  
//     if (e.target.tagName == 'SPAN' && e.target.classList.contains('icon-shanchu')) {
//         console.log('点击了删除按钮');
//         console.log(e.target.dataset.phone);
//         axios.post('/user/delete', {phone:e.target.dataset.phone}).then((res) => {
//             console.log(res.data);
//             render(res.data.list)
//         })
//     }
// } 

axios.post('/user/login', { phone: '15291695373', pass: '1234' }).then((res) => {
    console.log(res.data);
})

